﻿' Copyright (c) 2013  Christoc.com
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
Imports MVPcsIframe.Components
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Entities.Users


''' <summary>
''' The View class displays the content
''' 
''' Typically your view control would be used to display content or functionality in your module.
''' 
''' View may be the only control you have in your project depending on the complexity of your module
''' 
''' Because the control inherits from MVPcsIframeModuleBase you have access to any custom properties
''' defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
''' 
''' </summary>
Public Class Edit
    Inherits MVPcsIframeModuleBase

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Page_Load runs when the control is loaded
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Try
            Dim MultiURL As String = "False"
            If Not Page.IsPostBack Then

                If (Settings("MultiURL")) Then
                    MultiURL = Settings("MultiURL").ToString
                End If

                If ItemId > 0 Then
                    Load_Item(ItemId)
                ElseIf (MultiURL = "False") Then
                    Dim tc As New ItemController
                    If (tc.GetItems(ModuleId).Count > 0) Then
                        Load_Item(tc.GetItems(ModuleId).First.ItemId())
                    End If
                ElseIf (MultiURL = "True") Then


                End If
            End If
        Catch exc As Exception
            Exceptions.ProcessModuleLoadException(Me, exc)
        End Try

    End Sub

    Private Sub Load_Item(ByVal _itemID As Integer)

        Dim tc As New ItemController
        Dim t As Item = tc.GetItem(_itemID, ModuleId)
        If t IsNot Nothing Then
            txtName.Text = t.ItemName
            txtURL.Text = t.ItemURL
            txtDescription.Text = t.ItemDescription
            txtKey1.Text = t.Key1
            txtKey2.Text = t.Key2
            txtKey3.Text = t.Key3
            txtValue1.Text = t.Value1
            txtValue2.Text = t.Value2
            txtValue3.Text = t.Value3
        End If

    End Sub


    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        Dim t As New Item()
        Dim tc As New ItemController


        If ItemId > 0 Then
            t = tc.GetItem(ItemId, ModuleId)
        Else
            t.CreatedByUserId = UserId
            t.CreatedOnDate = DateTime.Now
        End If

        t.ItemName = txtName.Text.Trim()
        t.ItemURL = txtURL.Text.Trim()
        t.ItemDescription = txtDescription.Text.Trim()

        t.Key1 = txtKey1.Text.Trim()
        t.Key2 = txtKey2.Text.Trim()
        t.Key3 = txtKey3.Text.Trim()
        t.Value1 = txtValue1.Text.Trim()
        t.Value2 = txtValue2.Text.Trim()
        t.Value3 = txtValue3.Text.Trim()

        t.LastModifiedByUserId = UserId
        t.LastModifiedOnDate = DateTime.Now
        t.ModuleId = ModuleId
        t.TabId = TabId



        If t.ItemId > 0 Then
            tc.UpdateItem(t)
        Else
            tc.CreateItem(t)
        End If
        Response.Redirect(DotNetNuke.Common.Globals.NavigateURL())
    End Sub

    Protected Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect(DotNetNuke.Common.Globals.NavigateURL())
    End Sub

End Class